<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2020/12/31
 * Time: 10:31
 */

namespace App\Game\Logic;


use App\Game\Conf\GameProgress;
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
use App\Game\Core\AStrategy;
use App\Game\Core\Packet;
use App\Game\Core\Robot;
use App\Game\Timer\GameTimer;
use App\Service\Hupai;
use App\Service\Lock;
use App\Service\PaiXing;
use App\Task\GameSyncTask;
use App\Task\GameTimerTask;
use App\Task\OutRobotTask;

class CheckHPG extends AStrategy
{

    /**
     * 游戏检查胡碰杠
     */
    public function exec()
    {
        $game_conf = config('game');
        $account = $this->_params['userinfo']['account'];
        $user_room_map_key = sprintf($game_conf['user_room'], $account);
        $room_no = redis()->get($user_room_map_key);
        $inputData = $this->_params['data'];
        $type = isset($inputData['type']) ? $inputData['type'] : 0;
        $serv = server();
        $fd = getFdByAccount($account);
        if (empty($room_no)) {
            $msg = ['status' => 1, 'info' => '房间号不存在'];
            pushErr($serv, $fd, SubCmd::SUB_GAME_CHECK_HU_PENG_GANG, $msg);
            return;
        }
        if (!in_array($type, [0, 1, 2, 3])) {
            $msg = ['status' => 2, 'info' => '非法type值！'];
            pushErr($serv, $fd, SubCmd::SUB_GAME_CHECK_HU_PENG_GANG, $msg);
            return;
        }


        $room_data = getRoomData($room_no);

        if (!checkGameProgress($room_no, GameProgress::GAME_CHECK_HPG)) {
            $progress = getGameProgress($room_no);
            //出牌阶段，当前操作用户必须是先摸了牌后可以直接胡杠，不能碰
            if ($progress == GameProgress::GAME_OUT_CARD && $type) {
                $chair_id = getChairIdbyAccount($room_no, $account);
//                $inArr = json_decode($room_data['in'], true);
                if ($type == 3) { //碰牌拦截
                    return;
                }
                if (in_array($type, [1, 2])) {
                    $inArr = json_decode($room_data['in'], true);
                    $card = $inArr['card'];
                } else {
                    $card = $inputData['card'];
                }
                $hpgArr[$account] = $type;
                if ($type == 1) { //自摸胡牌
                    $user_data = json_decode($room_data[$account], true);
                    sort($user_data['card']);
                    $res = testHu($room_no, $inArr, $account, true);
                    if (!$res['status']) {
                        $res['data']['from_chair_id'] = $chair_id;
                        //广播胡牌
                        broadCast($room_no, $res['data'], SubCmd::SUB_GAME_HU_CARD_RESP);
                    }
                }
                if ($type == 2) { //自摸杠牌
                    $card = $inputData['card'];
                }
                ddTitle('do_check_hpk 0000000000  ');
                //清除前一个定时器杠牌非法，手牌数据有问题
                putTimerDel($room_no);
                GameTimer::do_check_hpk($room_no, $hpgArr, $card, true);
                return;
            }
            $msg = array('status' => 1, 'info' => '非法操作！');
            pushErr($serv, $fd, SubCmd::SUB_GAME_CHECK_HU_PENG_GANG, $msg);
            return;
        }
        //检查操作是否已经超时
        if (!checkCounter($room_no)) {
            $msg = ['status' => 1, 'info' => '该操作已超时，系统已自动为您执行默认操作'];
            pushErr($serv, $fd, SubCmd::SUB_GAME_CHECK_HU_PENG_GANG, $msg);
            return;
        }

        $chairs = getRoomChairs($room_no);
        $outArr = json_decode($room_data['out'], true);
        foreach ($chairs as $acc => $cid) {
            if ($cid != $outArr['chair_id']) { //参与胡碰杠的检测的三家
                $userData = json_decode($room_data[$acc], true);
                if ($userData['take_over']) { //如果托管，则自动过掉
                    ddTitle("take_over: $cid  account: $acc");
//                    redis()->hSet($room_check_hpg_key, (string)$acc, 0);
                    addCheckHpgArr($room_no, $acc, 0);
                }
            }
        }

        addCheckHpgArr($room_no, $account, $type);

        if ($type == 1) { //胡牌优先播放特效
            $user_data = json_decode($room_data[$account], true);
            $user_data['card'][] = $inputData['card'];
            sort($user_data['card']);
            $res = testHu($room_no, $inputData, $account);
            if (!$res['status']) {
                $res['data']['from_chair_id'] = $outArr['chair_id'];
                //广播胡牌
                broadCast($room_no, $res['data'], SubCmd::SUB_GAME_HU_CARD_RESP);
            }
        }

        $robots = Robot::getRoomRobot($room_no);
        $robotNum = count($robots);

        if ($robotNum == 3) { //单机
            ddTitle('do_check_hpg 111111  ');
            //清除前一个定时器
            putTimerDel($room_no);
            GameTimer::do_check_hpk($room_no, [], $inputData['card']);
            return;
        }

        $arr = getCheckHPGArr($room_no);
        $realNum = 4 - $robotNum - 1;
        dd('arr', $arr);
        dd('realNum', $realNum);
        if (count($arr) >= $realNum) {
            $user_checks = [];
            foreach ($arr as $k => $v) {
                $user_checks[$k] = $v;
            }
//            dd('user_checks', $user_checks);
            $index = 0;
            foreach ($user_checks as $acc => $v) {
                $index++;
                if ($index == $realNum) {
                    if ($acc == $account) {
                        ddTitle('do_check_hpg 22222222  ');
                        //清除前一个定时器
                        putTimerDel($room_no);
                        GameTimer::do_check_hpk($room_no, $user_checks, $inputData['card']);
                    }
                }
            }
        }
    }

    protected function resp($msg)
    {
        $fd = $this->_params['userinfo']['fd'];
        $serv = $this->_params['serv'];
        $data = Packet::packFormat('ERROR', 0, $msg);
        $return_data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::SUB_GAME_CHECK_HU_PENG_GANG);
        $serv->push($fd, $return_data, WEBSOCKET_OPCODE_BINARY);
    }


}